import { createProxy } from './src/config/proxyPath'
import { resolve } from 'path'
import { defineConfig, loadEnv } from 'vite'
import { createVuePlugin } from 'vite-plugin-vue2'
import { addBuildInfo } from './build/vite-addBuildInfo'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd())
  return {
    base: './',
    server: {
      host: '0.0.0.0',
      port: 9255,
      proxy: createProxy(env.VITE_ONLINE_API)
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@import "@/styles/var.scss";`
        }
      }
    },
    build: {
      assetsDir: 'static',
      emptyOutDir: true,
      reportCompressedSize: false,
      chunkSizeWarningLimit: 1500,
      rollupOptions: {
        output: {
          entryFileNames: 'static/js/[name]-[hash:8].js',
          chunkFileNames: 'static/common/[name]-[hash:8].js',
          assetFileNames: 'static/assets/[name]-[hash:8].[ext]',
          compact: true
        }
      }
    },
    resolve: {
      alias: {
        '@': resolve(__dirname, 'src')
      }
    },
    plugins: [
      createVuePlugin(),
      // 使用 svg 图标
      createSvgIconsPlugin({
        iconDirs: [resolve(process.cwd(), 'src/assets/svg')],
        symbolId: 'icon-[dir]-[name]'
      }),
      mode !== 'development' && addBuildInfo()
    ]
  }
})
